<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理" lazy>
          <el-button type="primary" size="small" @click="addClick">
            +新增角色
          </el-button>
          <el-table :data="list">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <el-button
                    type="text"
                    @click="clickPermission(row)"
                  >分配权限</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 3, 10, 20]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy>
          <Info />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <Add ref="add" @getData="getData" />

    <!-- 权限配置 -->
    <SetPermission ref="SetPermission" :is-show.sync="showPermission" />
  </div>
</template>

<script>
import { sysRole, sysRoleDelete } from '@/api/setting.js'
import Add from './components/add.vue'
import SetPermission from './components/setPermission.vue'
export default {
  name: '',
  components: {
    Info: () => import('@/views/setting/components/info.vue'),
    Add,
    SetPermission
  },
  data() {
    return {
      list: [],
      page: {
        page: 1, // 当前页
        pagesize: 10, // 当前页容量
        total: 100 // 总条数
      },
      show: false,
      showPermission: false
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    addClick() {
      this.$refs.add.isShow = true
      this.$refs.add.mode = 'add'
    },
    del(row) {
      // console.log(row)
      this.$confirm('确定删除吗？', '提示')
        .then(async() => {
          await sysRoleDelete(row.id)
          this.$message.success('删除成功')
          // 刷新数据并回到第一页
          this.handleCurrentChange(1)
        })
        .catch(() => {})
    },
    edit(row) {
      this.$refs.add.isShow = true
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.$refs.add.mode = 'edit'
    },
    handleSizeChange(val) {
      this.page.pagesize = val
      this.page.page = 1
      this.getData()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.getData()
      console.log(`当前页: ${val}`)
    },
    async getData() {
      const res = await sysRole(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
      console.log(res)
    },
    clickPermission(row) {
      this.showPermission = true

      this.$refs.SetPermission.getRoleInfo(row)
    }
  }
}
</script>
<style lang="less" scoped></style>
